package cn.senwill.controller;

import cn.senwill.domain.Result;
import cn.senwill.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/user") // 类级别的映射
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 注册
     * 127.0.0.1:8888/api/user/registry
     * {
     *     "username": "vegeta",
     *     "password": "123456"
     * }
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "registry")
    @ResponseBody
    @CrossOrigin
    public Result registry(@RequestBody JSONObject jsonObject) {
        return userService.registry(jsonObject);
    }

    /**
     * 登录
     * 127.0.0.1:8888/api/user/login
     * {
     *     "username": "vegeta",
     *     "password": "123456"
     * }
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "login")
    @ResponseBody
    @CrossOrigin
    public Result login(@RequestBody JSONObject jsonObject) {
        return userService.login(jsonObject);
    }

    /**
     * 获取用户信息
     * 127.0.0.1:8888/api/user/getUserInfo
     * @return
     */
    @GetMapping(value = "getUserInfo")
    @ResponseBody
    @CrossOrigin
    public Result getUserInfo(@RequestHeader("Authorization") String token) {
        return userService.getUserInfo(token);
    }
}